<template>
  <div class="app-container">
    <div class="crud-group">
      <el-input
        v-model="paramsForm.clusterName"
        placeholder="请输入账号"
        clearable
      />
      <el-input
        v-model="paramsForm.recipientPhone"
        placeholder="请输入手机号"
        clearable
      />
      <div>
        <el-button
          icon="el-icon-search"
          type="success"
          @click="getData"
        >搜索</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addDialogVisible = true"
        >新增</el-button>
      </div>
    </div>
    <el-table
      ref="table"
      v-loading="isLoading"
      :data="data"
      style="margin-top:10px"
    >
      <el-table-column
        type="index"
        label="序号"
        width="55"
      />
      <el-table-column
        label="姓名"
        prop="recipientRemarks"
      />
      <el-table-column
        label="账号"
        prop="recipientName"
      />
      <el-table-column
        label="手机号"
        prop="recipientPhone"
      />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="editData(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="delData(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        :current-page="paramsForm.current"
        :page-size="paramsForm.size"
        :page-sizes="[5, 10, 20, 30, 40]"
        layout="prev, pager, next,sizes,total"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <el-dialog
      :close-on-click-modal="false"
      :before-close="handleClose"
      :visible.sync="addDialogVisible"
      title="新增"
      width="800px"
    >
      <el-form
        ref="form"
        :inline="true"
        label-width="120px"
        :rules="rules"
        :model="addForm"
      >
        <el-form-item
          label="姓名:"
          prop="recipientRemarks"
        >
          <el-input
            v-model="addForm.recipientRemarks"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="账号:">
          <el-input
            v-model="addForm.recipientName"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="手机号:">
          <el-input
            v-model="addForm.recipientPhone"
            placeholder="请输入"
          />
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="handleClose"
        >取 消</el-button>
        <el-button
          type="primary"
          @click="addUser"
        >确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :close-on-click-modal="false"
      :before-close="editClose"
      :visible.sync="editFlag"
      title="修改"
      width="800px"
    >
      <el-form
        ref="forms"
        :inline="true"
        label-width="120px"
        :rules="rules"
        :model="editForm"
      >
        <el-form-item
          label="姓名:"
          prop="recipientRemarks"
        >
          <el-input
            v-model="editForm.recipientRemarks"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="账号:">
          <el-input
            v-model="editForm.recipientName"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="手机号:">
          <el-input
            v-model="editForm.recipientPhone"
            placeholder="请输入"
          />
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="editClose"
        >取 消</el-button>
        <el-button
          type="primary"
          @click="editUser"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import parameter from '@/api/phaseTwo/parameter.js'

export default {
  data() {
    return {
      total: 0,
      paramsForm: {
        current: 1,
        size: 10,
        recipientName: '',
        recipientPhone: ''
      },
      data: [],
      isLoading: false,
      addDialogVisible: false,
      permission: {
        add: ['admin', 'add'],
        edit: ['edit'],
        del: ['del']
      },
      addForm: {
        recipientRemarks: '',
        recipientName: '',
        recipientPhone: ''
      },
      sourceList: [],
      sendList: [{label: '微信', value: '1'}, {label: '钉钉', value: '2'}, {label: '微信,钉钉', value: '3'}],
      statusList: [{label: '已发布', value: '1'}, {label: '待发布', value: '2'}],
      topicList: [],
      editFlag: false,
      editForm: {},
      //  queryOptions: [
      //   {
      //     columnName: "source",
      //     columnValue: "来源类型",
      //     selectionData: [
      //         {
      //             key: "yarn",
      //             value: "yarn",
      //         },
      //         {
      //             key: "impala",
      //             value: "impala",
      //         },
      //         {
      //             key: "hive",
      //             value: "hive",
      //         },
      //     ],
      //     inputType: 2,
      //   },
      //   {
      //     columnName: "send",
      //     columnValue: "发送类型",
      //     selectionData: [
      //         {
      //             key: "vx",
      //             value: "企业微信",
      //         },
      //         {
      //             key: "dd",
      //             value: "钉钉",
      //         },
      //     ],
      //     inputType: 2,
      //   },
      //   {
      //     columnName: "status",
      //     columnValue: "发布状态",
      //     selectionData: [
      //         {
      //             key: "0",
      //             value: "已发布",
      //         },
      //         {
      //             key: "1",
      //             value: "待发布",
      //         },
      //     ],
      //     inputType: 2,
      //   },
      //   {
      //     columnName: "receiver",
      //     columnValue: "接收人",
      //     inputType: 1,
      //   },
      //  ],
      rules: {
        recipientRemarks: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {

  },
  created() {
    this.getData()
  },

  methods: {
    getData(resetPage) {
      if (resetPage) {
        this.paramsForm.current = 1
      }
      this.isLoading = true
      parameter.getWarningUserList(this.paramsForm).then((res) => {
        this.data = res.content
        this.total = res.totalElements
      }).finally(() => {
        this.isLoading = false
      })
    },
    handleSizeChange(val) {
      this.paramsForm.size = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.paramsForm.current = val
      this.getData(false)
    },
    handleClose() {
      this.addForm = {
        recipientRemarks: '',
        recipientName: '',
        recipientPhone: ''
      }
      this.addDialogVisible = false
    },
    editClose() {
      this.editForm = {}
      this.editFlag = false
    },
    editData(row) {
      this.editFlag = true
      const obj = JSON.stringify(row)
      this.editForm = JSON.parse(obj)
    },
    delData(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          parameter.delUser(row.id).then((res) => {
            this.$notify({
              title: '提示',
              message: '删除成功',
              type: 'success'
            })
            this.getData()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    addUser() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          parameter.addUser(this.addForm).then((res) => {
            if (res === 1) {
              this.$notify({
                title: '提示',
                message: '新增成功',
                type: 'success'
              })
            }
            this.handleClose()
            this.getData()
          })
        }
      })
    },
    editUser() {
      this.$refs['forms'].validate(valid => {
        if (valid) {
          parameter.editUser(this.editForm).then((res) => {
            if (res === 1) {
              this.$notify({
                title: '提示',
                message: '修改成功',
                type: 'success'
              })
              this.editClose()
              this.getData()
            }
          })
        }
      })
    }
  }
}
</script>
